package org.jeecg.modules.monitoring.j_monitoring.job.message;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.util.DateUtils;
import org.jeecg.modules.message.handle.impl.EmailSendMsgHandle;
import org.jeecg.modules.monitoring.j_monitoring.entity.MDailyInspection;
import org.jeecg.modules.monitoring.j_monitoring.service.IMDailyInspectionService;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.core.util.ApplicationContextUtil;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.*;

/**
 * 发送排班邮件
 * @Author: xiaoyq
 */

@Slf4j
public class SendDailyInspectionEmailJob implements Job {

    @Autowired
    private IMDailyInspectionService mDailyInspectionService;

    /***
     * 执行
     * @param jobExecutionContext
     * @throws JobExecutionException
     */
    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        log.info(String.format("发送每日的机房巡检邮件通知,时间:" + DateUtils.getTimestamp()));
        QueryWrapper<MDailyInspection> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(MDailyInspection::getInspectionDate,DateUtils.str2Date(DateUtils.formatDate(DateUtils.getDate(),"yyyy-MM-dd"),DateUtils.date_sdf.get()));
        List<MDailyInspection> dataList = mDailyInspectionService.list(queryWrapper);
        Map<String,MDailyInspection> map = new HashMap<>();
        for (int i = 0; i < dataList.size(); i++) {
            if(!map.containsKey(dataList.get(i).getPersonnelId())){
                map.put(dataList.get(i).getPersonnelId(),dataList.get(i));
            }
        }

        List<String> idList = new ArrayList<>();
        List<SysUser> userList = null;


        List<String> finalIdList = idList;
        map.forEach((key, item)->{
            if(StringUtils.isNotBlank(key)){
                finalIdList.addAll(Arrays.asList(key.split(",")));
            }
        });

        if(idList.size()>0){
            ISysUserService userService = ApplicationContextUtil.getContext().getBean(ISysUserService.class);
            userList = userService.listByIds(idList);
        }

        if(userList!=null && userList.size()>0){
            EmailSendMsgHandle emailHandle=new EmailSendMsgHandle();
            SysUser user;
            for (int i = 0; i < userList.size(); i++) {
                user = userList.get(i);
                if(StringUtils.isNotBlank(user.getEmail())){
                    try{
                        String email = user.getEmail().trim();
                        String title = "中国海油-数据中心巡检工作提醒";
                        StringBuilder msg = new StringBuilder();
                        msg.append(user.getRealname()+":\n\n");
                        msg.append("    您好，您今天"+ DateUtils.getDate("yyyy-MM-dd") + "有巡检工作,请提前做好工作安排。");
                        emailHandle.sendMsg(email, title, msg.toString());
                    }
                    catch (Exception ex){

                    }

                }
                else{
                    log.info(user.getRealname() + "未设置电子邮箱.");
                }
            }

        }

        log.info(String.format("每日的机房巡检邮件通知结束."));
    }

}
